package com.jwl.idc.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jwl.idc.R;
import com.jwl.idc.actionbean.BaseBean;
import com.jwl.idc.customview.DialogCreater;
import com.jwl.idc.customview.TitleBar;
import com.jwl.idc.http.HttpAction;
import com.jwl.idc.http.HttpToViewInterface;
import com.jwl.idc.utils.CountDownTime;
import com.jwl.idc.utils.StringUtils;

/**
 * Created by liangfan on 2016/1/5.
 * descript : 注册页面
 */
public class RegisterUserActivity extends BaseActivity implements View.OnClickListener {
    public HttpAction action = null;
    public HttpToViewInterface httpToviewCallBack = null;

    private EditText inputPhonenumberEt, inputNickNameEt, inputPwdEt, inputYzmEt;
    private CheckBox pwdCb;
    private TextView yzmTv;
    private Button registerUserBt;
    private CountDownTime countDownTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);
        initView();


    }

    void initView() {
        titlebar = (TitleBar) findViewById(R.id.titleBar);
        titlebar.setClickListener(this);
        titlebar.setText(getString(R.string.back), getString(R.string.register_user), "");
        inputPhonenumberEt = (EditText) findViewById(R.id.inputPhoneNumberEt);
        inputNickNameEt = (EditText) findViewById(R.id.inputNikeNameEt);
        registerUserBt = (Button) findViewById(R.id.registerUserBt);
        inputPwdEt = (EditText) findViewById(R.id.inputPwdEt);
        inputYzmEt = (EditText) findViewById(R.id.inputYzmEt);
        pwdCb = (CheckBox) findViewById(R.id.pwdCb);
        yzmTv = (TextView) findViewById(R.id.yzmTv);
        pwdCb.setOnClickListener(this);
        yzmTv.setOnClickListener(this);
        registerUserBt.setOnClickListener(this);
    }

    //获取验证码
    void getYzm(String phoneNumber) {
        httpToviewCallBack = new HttpToViewInterface() {
            @Override
            public void callBackData(BaseBean data) {
            }

            @Override
            public void finish() {

            }

        };
        action = new HttpAction(httpToviewCallBack, this);
        action.getSMS(phoneNumber);
    }

    //注册操作
    void registerUser(String phoneNumber, String pwd, String sms, String userName) {
        httpToviewCallBack = new HttpToViewInterface() {
            @Override
            public void callBackData(BaseBean data) {
                Toast.makeText(RegisterUserActivity.this,RegisterUserActivity.this.getString(R.string.register_success),Toast.LENGTH_LONG);
                finish();
            }
            @Override
            public void finish() {
                DialogCreater.closeDialog(BaseActivity.REGISER_USER_DIALOG);
            }
        };
        action = new HttpAction(httpToviewCallBack, this);
        action.registerUser(phoneNumber, pwd, sms, userName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titleBackTv:
                finish();
                break;
            case R.id.yzmTv:
                String phoneNumber = inputPhonenumberEt.getText().toString().trim();
                if (StringUtils.checkPhoneNumber(phoneNumber, this)) {
                    getYzm(phoneNumber);
                    //开始倒计时
                    if (countDownTime == null)
                        countDownTime = new CountDownTime(yzmTv);
                    countDownTime.start(120);
                }
                break;
            case R.id.pwdCb:
                if (pwdCb.isChecked()) {
                    inputPwdEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    inputPwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
            case R.id.registerUserBt:
                String number = inputPhonenumberEt.getText().toString().trim();
                String pwd = inputPwdEt.getText().toString().trim();
                String yzm = inputYzmEt.getText().toString().trim();
                String nikeName = inputNickNameEt.getText().toString().trim();
                if (StringUtils.checkPhoneNumber(number, this)
                        && StringUtils.checkPwd(pwd, this)
                        && StringUtils.checkNickName(nikeName, this)
                        && StringUtils.checkYzm(yzm, this)) {
                    registerUser(number, pwd, yzm, nikeName);
                    showWaitDialog(REGISER_USER_DIALOG,getString(R.string.registering));
                }
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        inputPhonenumberEt = null;
        inputNickNameEt = null;
        inputPwdEt = null;
        inputYzmEt = null;
        pwdCb = null;
        if (countDownTime != null)
            countDownTime.close();
        countDownTime = null;
    }
}
